package com.jerry.template.module.mainpage.shouye.duanzi.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.template.R;
import com.jerry.template.base.BaseFragment;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mainpage.shouye.duanzi.adapter.ActivityJokeListContentAdapter;
import com.jerry.template.module.mainpage.shouye.duanzi.entity.DZDetail;
import com.jerry.template.utils.AsyncTask;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class EasyJokeFragment extends BaseFragment {

    @BindView(R.id.joke_list)
    PullToRefreshListView listView;

    private ActivityJokeListContentAdapter mAdapter = null;

    private List<DZDetail.DataBean.DataBean1> mDataBean = new ArrayList<>();

    private int index = 1;

    FragmentManager mFragmentManager = null;

    public EasyJokeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_joke_list, container, false);
    }
    @Override
    protected void initViews() {
        mAdapter = new ActivityJokeListContentAdapter(getContext(),mDataBean);
        listView.setAdapter(mAdapter);
        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                index = 1;
                mDataBean.clear();
                loadData();
                mAdapter.notifyDataSetChanged();
                new FinishRefresh().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                index++;
                loadData();
                mAdapter.notifyDataSetChanged();
                new FinishRefresh().execute();
            }
        });
    }

    private class FinishRefresh extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            listView.onRefreshComplete();
        }
    }


    @Override
    protected void loadData() {
        Map<String,Object> params = new HashMap<>();
        params.put("essence",index);
        OkHttpUtils.postSubmitForm(Urls.JOKE_LIST,null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        super.postEventResult(message);

        if (message != null) {
            if (message.type.equals(Urls.JOKE_LIST)) {
                String result = message.result;
                Gson gson = new Gson();
                DZDetail dzDetail = gson.fromJson(result,DZDetail.class);
                mDataBean.addAll(dzDetail.getData().getData());
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }
}
